package com.jeason.fileserver.controller;

import com.jeason.fileserver.domain.FileForm;
import com.jeason.fileserver.entity.FileBean;
import com.jeason.fileserver.service.FileService;
import com.jeason.fileserver.utils.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * (File)表控制层
 *
 * @author makejava
 * @since 2021-01-17 13:41:04
 */
@RestController
@RequestMapping("/admin/file")
public class FileController {
    /**
     * 服务对象
     */
    @Resource
    private FileService fileService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public FileBean selectOne(Integer id) {
        return this.fileService.queryById(id);
    }

    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                           @RequestParam(value = "limit", required = false, defaultValue = "6") Integer limit) {

        return AjaxResult.success(fileService.queryAllByLimit(page - 1, limit));
    }

    @GetMapping("/searchByKeyword")
    public AjaxResult list(@RequestParam(value = "keyword") String keyword) {

        return AjaxResult.success(fileService.searchByKeyword(keyword));
    }

    @GetMapping("/searchByTime")
    public AjaxResult searchByTime(@RequestParam(value = "beginTime") Date beginTime,
                           @RequestParam(value = "endTime") Date endTime) {

        return AjaxResult.success(fileService.searchByTime(beginTime, endTime));
    }

    @PostMapping("/updateFileName")
    public AjaxResult updateFileName(@RequestBody FileForm fileForm) {

        return AjaxResult.success(fileService.updateFileName(fileForm.getFileName(), fileForm.getFilePath()));
    }

    @DeleteMapping("/deleteFileById")
    public AjaxResult deleteFileById(@RequestParam(value = "id") String id) {
        return AjaxResult.success(fileService.deleteFileById(Integer.valueOf(id)));
    }

    @GetMapping("/downloadFileById")
    public AjaxResult downloadFileById(HttpServletResponse response, @RequestParam(value = "id") String id) {
        return AjaxResult.success(fileService.downloadFileById(response, Integer.valueOf(id)));
    }

}
